jQuery(function($) {
	var name = $.cookie("name");
	var pw = $.cookie("pw");
	
	//自动填充记住的密码
	if( name && pw && name != "" && pw != "" ){
		$("#remember-me").attr("checked",true);
		$("#email").val(name);
		$("#password").val(pw);
	}
	
	
	$('#btn-login-dark').on('click', function(e) {
		$('body').attr('class', 'login-layout');
		$('#id-text2').attr('class', 'white');
		$('#id-company-text').attr('class', 'blue');

		e.preventDefault();
	});
	$('#btn-login-light').on('click', function(e) {
		$('body').attr('class', 'login-layout light-login');
		$('#id-text2').attr('class', 'grey');
		$('#id-company-text').attr('class', 'blue');

		e.preventDefault();
	});
	$('#btn-login-blur').on('click', function(e) {
		$('body').attr('class', 'login-layout blur-login');
		$('#id-text2').attr('class', 'white');
		$('#id-company-text').attr('class', 'light-blue');

		e.preventDefault();
	});

	$('#nextStep').click(function(e){
		var msg = "";
		var _email = $('#email').val();
		if(_email=='') {
			msg += '邮箱不能为空<br/>';
		} else {
			$.ajax({
				dataType: "json",
				url: "/admin/exists",
				data: "key=" + _email,
				async: false,
				success: function(_msg){
					if(_msg.exist){
						msg +=  (_email + "已存在!<br/>");
					}
				}
			});
		}
		if($('#passwd').val()=='') {
			msg += '密码不能为空<br/>';
		}
		if(!document.getElementById("accept").checked) {
			msg += '请接受用户协议';
		}
		$('#_msg').addClass('alert alert-block alert-danger').html(msg);
		if(msg == '') {
			$('#regist').submit();
		}
	});
	
	//重新加载验证码
	function reloadVerifyImg(){
		var $img = $("#verify-img");
		var verifyImgUrl = $img.attr("src");
		var rnd = Math.random();
		$img.attr("src",verifyImgUrl + "?rnd=" + rnd);
	}
	
	$("#verify-img").click(reloadVerifyImg);
	isValide = false;
	
	//校验验证码
	$("#verify-code-input").keyup(function(e){
		var $this = $(this);
		var code = $this.val();
		var msg = "";
		
		if(code.length == 6 ){
			$.ajax({
				dataType: "json",
				url: "/admin/valideCaptchaCode",
				data: "vCode=" + code,
				async: false,
				success: function(_msg){
					if(_msg.valide){
						isValide = true;
						$("#verify-icon").removeClass("glyphicon-remove").removeClass("glyphicon-remove").addClass("glyphicon-ok").css("visibility","visible");
						if(e.keyCode == 13){
							$("#loginBtn").trigger("click");
						}
					}else{
						msg = "校验码输入错误";
						$("#verify-icon").removeClass("glyphicon-ok").addClass("glyphicon-remove").css("visibility","visible");
						$('#_msg').addClass('alert-block').css("color","red").html(msg);
					}
					
				},
				error: function(_msg){
					msg = "服务器异常， 请稍候再试!";
					$('#_msg').addClass('alert-block').html(msg);
				}
			});
		}else{
			$("#verify-icon").css("visibility","hidden");
			isValide = false;
		}
		
	});
	
	function showErr(msg){
		$('#_msg').addClass('alert-block').html(msg);
	}
	
	//登录
	$("#loginBtn").click(function(){
		var msg = '';
		if( $.trim( $("#email").val() ).length == 0 ){
			msg = "账户不能为空<br/>";
			showErr(msg);
			return false;
		}
		
		if( $.trim( $("#password").val() ).length == 0 ){
			msg =   "密码不能为空<br/>";
			showErr(msg);
			return false;
		}
		
		if($.trim($("#verify-code-input").val()).length == 0){
			msg =   "请输入校验码<br/>";
			showErr(msg);
			return false;
		}
		
		if(!isValide){
			msg =   "校验码错误<br/>";
			showErr(msg);
			return false;
		}
		  
		// 提交
		$.ajax({ 
			dataType: "json",
			type: "post",
			url: "/admin/login",
			data: {"email" :  $.trim($("#email").val()) , "password" : $.trim($("#password").val())},
			async: false,
			success: function(_msg){
				if(_msg.isLogin == '1'){
					rememberMe($.trim($("#email").val()),$.trim($("#password").val()));
					window.location.href = _msg.go;
				}else{
					msg = "用户名或密码错误";
					$("#password").html();
					$('#_msg').html(msg);
				}
			},
			error: function(_msg){
				msg = "服务器响应失败";
				$('#_msg').html(msg);
			}
		});
	});
	
	/**
	 * 记住账号和密码
	 */
	function rememberMe(name,pw){
		var isCheck = $("#remember-me").is(':checked');
		if(isCheck === true){
			$.cookie("name",name,{ expires: 365, path: '/' });
			//FIXME 此处可对密码进行加密
			$.cookie("pw",pw,{ expires: 365, path: '/' });
		}else{
			$.removeCookie("name",{ path: '/' });
			$.removeCookie("pw",{ path: '/' });
		}
	}
		
});